<template>
  <div class="employees">
    <el-card class="btnRight">
      <el-button type="primary" @click="importAdd">导入</el-button>
      <el-button type="primary" @click="add">+ 新增员工</el-button>
    </el-card>
    <el-card>
      <el-table :data="list">
        <el-table-column label="序号">
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.size + $index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="username"></el-table-column>
        <el-table-column label="头像" prop="staffPhoto">
          <template v-slot="{ row }">
            <img
              @click="imgClick(row.staffPhoto)"
              :src="row.staffPhoto"
              v-imgError="errImage"
              class="avatar"
            />
          </template>
        </el-table-column>
        <el-table-column label="手机号" prop="mobile"></el-table-column>
        <el-table-column label="工号" prop="workNumber"></el-table-column>
        <el-table-column label="聘用形式" prop="formOfEmployment">
          <template v-slot="{ row }">
            <div>{{ row.formOfEmployment | formater }}</div>
          </template>
        </el-table-column>
        <el-table-column label="部门" prop="departmentName"></el-table-column>
        <el-table-column label="入职时间" prop="timeOfEntry"
          ><template v-slot="{ row }">
            <div>
              {{ row.timeOfEntry | formateTime }}
            </div>
          </template></el-table-column
        >
        <!-- <el-table-column label="是否在职" prop="username"></el-table-column> -->
        <el-table-column label="状态" prop="enableState"></el-table-column>
        <el-table-column label="操作" width="300" fixed="right">
          <template v-slot="{ row }">
            <div>
              <el-button type="text" @click="check(row.id)">查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text">离职</el-button>
              <el-button type="text" @click="clickRole(row.id)">角色</el-button>
              <el-button type="text" @click="del(row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[1, 5, 10, 100, 200, 300, 400]"
          layout="total, sizes, prev, pager, next, jumper"
          :current-page="page.page"
          :page-size="page.size"
          :total="page.total"
        >
        </el-pagination>
      </div>
    </el-card>
    <DialogItem
      :show.sync="isShow"
      @handleCurrentChange="handleCurrentChange"
    ></DialogItem>
    <!-- 头像二维码 -->
    <el-dialog title="二维码" :visible.sync="showQrcode">
      <div style="text-align:center">
        <canvas ref="canvas"></canvas>
      </div>
    </el-dialog>
    <!-- 分配角色弹框组件 -->
    <DialogRole ref="dialogRole"></DialogRole>
  </div>
</template>

<script>
import { sysUser, sysUserDel } from '@/api/employees.js'
import DialogItem from './components/dialogItem.vue'
import Qrcode from 'qrcode'
import DialogRole from './components/dialogRole.vue'
export default {
  components: { DialogItem, DialogRole },
  data () {
    return {
      showQrcode: false,
      errImage: require('@/assets/common/head.jpg'),
      isShow: false,
      list: [],
      page: {
        total: 0,
        page: 1,
        size: 5
      }
    }
  },
  created () {
    this.getData()
  },
  methods: {
    async getData () {
      const res = await sysUser(this.page)
      this.list = res.data.rows
      this.page.total = res.data.total
      console.log('员工简单列表', res)
    },
    handleSizeChange (size) {
      this.page.size = size
      this.page.page = 1
      this.getData()
    },
    handleCurrentChange (page) {
      this.page.page = page
      this.getData()
    },
    // 新增员工
    add () {
      this.isShow = true
    },
    // 删除员工
    del (id) {
      this.$confirm('您确定要删除这位员工吗', '提示')
        .then(async () => {
          await sysUserDel(id)
          this.$message.success('删除员工成功')
          this.handleCurrentChange(1)
        })
        .catch(() => {})
    },
    //导入员工
    importAdd () {
      this.$router.push('/employees/import?name=' + encodeURIComponent('员工'))
    },
    // 查看员工
    check (id) {
      this.$router.push('/employees/details/' + id)
    },
    // 点击图片生成二维码
    imgClick (imgUrl) {
      if (!imgUrl) {
        this.$message.error('暂无图片')
        return
      }
      this.showQrcode = true
      this.$nextTick(() => {
        Qrcode.toCanvas(this.$refs.canvas, imgUrl)
      })
    },
    // 角色按钮
    clickRole (id) {
      this.$refs.dialogRole.show = true
      // 传值并调用子组件方法
      this.$refs.dialogRole.getUserInfo(id)
    }
  }
}
</script>

<style lang="scss" scoped>
.employees {
  padding: 15px;
  .btnRight {
    text-align: right;
    margin-bottom: 10px;
  }
  .pagination {
    text-align: right;
    padding-top: 30px;
  }
  .avatar {
    width: 50px;
    height: 50px;
    object-fit: contain;
  }
}
</style>
